import { Component, OnInit, ViewChild } from '@angular/core'
import { JsonApiService } from 'app/core/api/json-api.service' //http请求配置引入
import { config } from '../../shared/smartadmin.config'

declare var $: any
@Component({
  selector: 'sa-statistical-download',
  templateUrl: './statistical-download.component.html'
})
export class StatisticalDownloadComponent implements OnInit {
  startTime: string = '' //开始时间
  endTime: string = '' //结束时间
  companyIds: any = []
  companyArrays: any = []
  inputApi: string = 'company/getParentCompany.html'
  orderType: string = ''

  secondSearch = {
    startTime: '', //开始时间
    endTime: '' //结束时间
  }

  constructor(private jsonApiService: JsonApiService) {}

  ngOnInit() {
    this.initDate().then()
  }
  //数据的导出
  dowmload(type) {
    if (this.companyIds.length == 0) {
      this.jsonApiService.message('请先选择所属厂区')
      return
    }
    this.startTime = $('#datetimepicker1').val()
    this.endTime = $('#datetimepicker2').val()
    if (this.startTime == '' || this.endTime == '') {
      this.jsonApiService.message('起始结束时间不能为空')
      return
    }
    if (!this.orderType) {
      this.jsonApiService.message('请选择统计方式')
      return
    }
    let timeCheck = common.correctTimeParams(this.startTime, this.endTime, 0)
    if (!timeCheck.status) {
      this.jsonApiService.message(timeCheck.message)
      return
    }
    let urlPath = ''
    if (type === 1) {
      urlPath = 'renewalStatisticsV2/carDayActive.html'
    } else if (type === 2) {
      urlPath = 'renewalStatisticsV2/carOrderReceiving.html'
    } else if (type === 3) {
      urlPath = 'renewalStatisticsV2/carMonthActive.html'
    }
    const reqdata = {
      startTime: this.startTime,
      endTime: this.endTime,
      companyIds: this.companyIds,
      type: 1,
      orderType: this.orderType
    }
    common.exprotDataFile(reqdata, 'data', config.BaseURL + urlPath)
    // const url = urlPath + '?startTime=' + this.startTime + '&endTime=' + this.endTime + '&companyIds=' + this.companyIds + '&type=1&orderType=' + this.orderType;
    // window.location.href = config.BaseURL + url;
  }
  //数据的导出
  downloadSecond(type) {
    this.secondSearch.startTime = $('#datetimepicker3').val()
    this.secondSearch.endTime = $('#datetimepicker4').val()
    if (this.secondSearch.startTime == '' || this.secondSearch.endTime == '') {
      this.jsonApiService.message('起始结束时间不能为空')
      return
    }
    let timeCheck = common.correctTimeParams(this.secondSearch.startTime, this.secondSearch.endTime, 0)
    if (!timeCheck.status) {
      this.jsonApiService.message(timeCheck.message)
      return
    }
    let urlPath = ''
    if (type === 1) {
      urlPath = 'materialStatistics/exportCarMaterial.html'
    } else if (type === 2) {
      urlPath = 'materialStatistics/deviceRepairedAccountExcel.html'
    } else if (type === 3) {
      urlPath = 'materialStatistics/deviceEventExcel.html'
    } else if (type === 4) {
      urlPath = 'materialStatistics/exportAllMaterialSend.html'
    } else if (type === 5) {
      urlPath = 'materialStatistics/exportAllMaterialReplenishment.html'
    }

    const url =
      config.BaseURL + urlPath + '?startTime=' + this.secondSearch.startTime + '&endTime=' + this.secondSearch.endTime
    window.location.href = url
    // const url = urlPath + '?startTime=' + this.startTime + '&endTime=' + this.endTime + '&companyIds=' + this.companyIds + '&type=1&orderType=' + this.orderType;
    // window.location.href = config.BaseURL + url;
  }
  initDate() {
    var that = this
    return new Promise(function (resolve, reject) {
      System.import('assets/js/datepicker/js/bootstrap-datetimepicker.min.js').then(() => {
        System.import('assets/js/datepicker/locales/bootstrap-datetimepicker.zh-CN.js').then(() => {
          $('#datetimepicker1')
            .datetimepicker({
              //时间插件
              format: 'yyyy-mm-dd hh:ii:ss',
              language: 'zh-CN',
              autoclose: true
            })
            .on('changeDate', function (e) {
              var date = e.date
              var time = common.getTimeMmss(date)
              $('#datetimepicker2').datetimepicker('setStartDate', time)
            })
          $('#datetimepicker2')
            .datetimepicker({
              //时间插件
              format: 'yyyy-mm-dd hh:ii:ss',
              language: 'zh-CN',
              autoclose: true
            })
            .on('changeDate', function (e) {
              var date = e.date
              var time = common.getTimeMmss(date)
              $('#datetimepicker1').datetimepicker('setEndDate', time)
            })
          $('#datetimepicker3')
            .datetimepicker({
              //时间插件
              format: 'yyyy-mm-dd hh:ii:ss',
              language: 'zh-CN',
              autoclose: true
            })
            .on('changeDate', function (e) {
              var date = e.date
              var time = common.getTimeMmss(date)
              $('#datetimepicker2').datetimepicker('setStartDate', time)
            })
          $('#datetimepicker4')
            .datetimepicker({
              //时间插件
              format: 'yyyy-mm-dd hh:ii:ss',
              language: 'zh-CN',
              autoclose: true
            })
            .on('changeDate', function (e) {
              var date = e.date
              var time = common.getTimeMmss(date)
              $('#datetimepicker3').datetimepicker('setEndDate', time)
            })
          // var date = new Date();
          // $("#datetimepicker2").val(that.getTime(date)); //当前时间
          // date.setDate(date.getDate() - 7); //设置前一周
          // $("#datetimepicker1").val(that.getTime(date));
          resolve()
        })
      })
    })
  }
  //删除所选时间
  del_date() {
    this.startTime = ''
    this.endTime = ''
    $('#datetimepicker1').val('').change()
    $('#datetimepicker2').val('').change()
    $('#datetimepicker2').datetimepicker('setStartDate', null)
    $('#datetimepicker1').datetimepicker('setEndDate', null)
  }
  //删除所选时间
  del_date_back() {
    this.secondSearch.startTime = ''
    this.secondSearch.endTime = ''
    $('#datetimepicker3').val('').change()
    $('#datetimepicker4').val('').change()
    $('#datetimepicker4').datetimepicker('setStartDate', null)
    $('#datetimepicker3').datetimepicker('setEndDate', null)
  }
  addRecordCompanySelected(data) {
    if (data.companyIdSelectedList.length > 0) {
      this.companyIds = data.companyIdSelectedList.join(',')
    }
  }
  //点击对接厂区
  companySelectedClick() {
    $('#recordPopupCompany .s_tree_box.s_tree_box_width').addClass('tree_open')
  }
}
